<template>
	<view class="container">
		
		<!-- 设置列表 -->
		<view class="settings-list">
			<!-- 账号安全 -->
			<view class="settings-group">
				<view class="group-title">账号安全</view>
				<view class="menu-item" @tap="openPhonePopup">
					<view class="menu-item-left">
						<!-- <image src="https://jf2411-dd.oss-cn-hangzhou.aliyuncs.com/return.png" mode="aspectFit" style="width: 30px;height: 30px;"></image> -->
						<text>手机号</text>
					</view>
					<view class="menu-item-right">
						<text class="value">{{formatPhone(userInfo.phone)}}</text>
						<image src="https://jf2411-dd.oss-cn-hangzhou.aliyuncs.com/enter.png" mode="aspectFit" style="width: 30px;height: 30px;"></image>
					</view>
				</view>
				<view class="menu-item" @tap="openPasswordPopup">
					<view class="menu-item-left">
						<!-- <image src="https://jf2411-dd.oss-cn-hangzhou.aliyuncs.com/return.png" mode="aspectFit" style="width: 30px;height: 30px;"></image> -->
						<text>修改密码</text>
					</view>
					<image src="https://jf2411-dd.oss-cn-hangzhou.aliyuncs.com/enter.png" mode="aspectFit" style="width: 30px;height: 30px;"></image>
				</view>
			</view>
			
			<!-- 紧急联系人 -->
			<view class="settings-group">
				<view class="group-title">紧急联系人</view>
				<view class="menu-item" @tap="openContactPopup">
					<view class="menu-item-left">
						<!-- <image src="https://jf2411-dd.oss-cn-hangzhou.aliyuncs.com/return.png" mode="aspectFit" style="width: 30px;height: 30px;"></image> -->
						<text>紧急联系人</text>
					</view>
					<view class="menu-item-right">
						<text class="value">{{emergencyContact.name || '未设置'}}</text>
						<image src="https://jf2411-dd.oss-cn-hangzhou.aliyuncs.com/enter.png" mode="aspectFit" style="width: 30px;height: 30px;"></image>
					</view>
				</view>
			</view>
		</view>
		
		<!-- 退出登录按钮 -->
		<view class="logout-btn" @tap="handleLogout">
			退出登录
		</view>
		
		<!-- 遮罩层 -->
		<view class="mask" v-if="showMask" @tap="closeAllPopup"></view>
		
		<!-- 修改手机号弹窗 -->
		<view class="popup" v-if="showPhonePopup">
			<view class="popup-content">
				<view class="popup-header">
					<text class="popup-title">修改手机号</text>
					<view class="close-btn" @tap="closePhonePopup">×</view>
				</view>
				<view class="input-group">
					<input type="number" v-model="newPhone" placeholder="请输入新手机号" maxlength="11" />
					<view class="verify-code">
						<input type="number" v-model="verifyCode" placeholder="验证码" maxlength="6" />
						<button class="code-btn" :disabled="counting > 0" @tap="sendVerifyCode">
							{{counting > 0 ? `${counting}s` : '获取验证码'}}
						</button>
					</view>
				</view>
				<button class="confirm-btn" @tap="confirmChangePhone">确认修改</button>
			</view>
		</view>
		
		<!-- 修改密码弹窗 -->
		<view class="popup" v-if="showPasswordPopup">
			<view class="popup-content">
				<view class="popup-header">
					<text class="popup-title">修改密码</text>
					<view class="close-btn" @tap="closePasswordPopup">×</view>
				</view>
				<view class="input-group">
					<input type="password" v-model="oldPassword" placeholder="请输入原密码" />
					<input type="password" v-model="newPassword" placeholder="请输入新密码" />
					<input type="password" v-model="confirmPassword" placeholder="请确认新密码" />
				</view>
				<button class="confirm-btn" @tap="confirmChangePassword">确认修改</button>
			</view>
		</view>
		
		<!-- 紧急联系人弹窗 -->
		<view class="popup" v-if="showContactPopup">
			<view class="popup-content">
				<view class="popup-header">
					<text class="popup-title">紧急联系人</text>
					<view class="close-btn" @tap="closeContactPopup">×</view>
				</view>
				<view class="input-group">
					<input type="text" v-model="contactName" placeholder="请输入联系人姓名" />
					<input type="number" v-model="contactPhone" placeholder="请输入联系人手机号" maxlength="11" />
					<input type="text" v-model="contactRelation" placeholder="请输入与联系人关系" />
				</view>
				<button class="confirm-btn" @tap="confirmEmergencyContact">确认保存</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: {
					phone: '13800138000'
				},
				emergencyContact: {
					name: '',
					phone: '',
					relation: ''
				},
				// 弹窗状态
				showMask: false,
				showPhonePopup: false,
				showPasswordPopup: false,
				showContactPopup: false,
				// 修改手机号相关
				newPhone: '',
				verifyCode: '',
				counting: 0,
				// 修改密码相关
				oldPassword: '',
				newPassword: '',
				confirmPassword: '',
				// 紧急联系人相关
				contactName: '',
				contactPhone: '',
				contactRelation: ''
			}
		},
		onShow() {
			this.getUserInfo()
			this.getEmergencyContact()
			
		},
		methods: {
			// 返回上一页
			goBack() {
				uni.navigateBack()
			},
			// 获取用户信息
			getUserInfo() {
				// TODO: 调用后端API获取用户信息
			},
			// 获取紧急联系人信息
			getEmergencyContact() {
				// TODO: 调用后端API获取紧急联系人信息
			},
			// 格式化手机号显示
			formatPhone(phone) {
				if (!phone) return '未设置'
				return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
			},
			// 关闭所有弹窗
			closeAllPopup() {
				this.showMask = false
				this.showPhonePopup = false
				this.showPasswordPopup = false
				this.showContactPopup = false
				this.clearFormData()
			},
			// 清空表单数据
			clearFormData() {
				this.newPhone = ''
				this.verifyCode = ''
				this.oldPassword = ''
				this.newPassword = ''
				this.confirmPassword = ''
				this.contactName = ''
				this.contactPhone = ''
				this.contactRelation = ''
			},
			// 显示弹窗方法
			openPhonePopup() {
				this.showMask = true
				this.showPhonePopup = true
			},
			openPasswordPopup() {
				this.showMask = true
				this.showPasswordPopup = true
			},
			openContactPopup() {
				this.showMask = true
				this.showContactPopup = true
				this.contactName = this.emergencyContact.name
				this.contactPhone = this.emergencyContact.phone
				this.contactRelation = this.emergencyContact.relation
			},
			// 关闭弹窗方法
			closePhonePopup() {
				this.closeAllPopup()
			},
			closePasswordPopup() {
				this.closeAllPopup()
			},
			closeContactPopup() {
				this.closeAllPopup()
			},
			// 发送验证码
			sendVerifyCode() {
				if (!this.newPhone || this.newPhone.length !== 11) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					})
					return
				}
				// TODO: 调用发送验证码API
				this.counting = 60
				const timer = setInterval(() => {
					this.counting--
					if (this.counting <= 0) {
						clearInterval(timer)
					}
				}, 1000)
			},
			// 确认修改手机号
			confirmChangePhone() {
				if (!this.newPhone || !this.verifyCode) {
					uni.showToast({
						title: '请填写完整信息',
						icon: 'none'
					})
					return
				}
				// TODO: 调用修改手机号API
				this.closeAllPopup()
				uni.showToast({
					title: '修改成功'
				})
			},
			// 确认修改密码
			confirmChangePassword() {
				if (!this.oldPassword || !this.newPassword || !this.confirmPassword) {
					uni.showToast({
						title: '请填写完整信息',
						icon: 'none'
					})
					return
				}
				if (this.newPassword !== this.confirmPassword) {
					uni.showToast({
						title: '两次密码输入不一致',
						icon: 'none'
					})
					return
				}
				// TODO: 调用修改密码API
				this.closeAllPopup()
				uni.showToast({
					title: '修改成功'
				})
			},
			// 确认保存紧急联系人
			confirmEmergencyContact() {
				if (!this.contactName || !this.contactPhone || !this.contactRelation) {
					uni.showToast({
						title: '请填写完整信息',
						icon: 'none'
					})
					return
				}
				// TODO: 调用保存紧急联系人API
				this.emergencyContact = {
					name: this.contactName,
					phone: this.contactPhone,
					relation: this.contactRelation
				}
				this.closeAllPopup()
				uni.showToast({
					title: '保存成功'
				})
			},
			// 退出登录
			handleLogout() {
				uni.showModal({
					title: '提示',
					content: '确定要退出登录吗？',
					success: (res) => {
						if (res.confirm) {
							// TODO: 调用退出登录API
							uni.reLaunch({
								url: '/pages/accLogin/accLogin'
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
.container {
	min-height: 100vh;
	background-color: #f5f5f5;
}

.nav-bar {
	height: 88rpx;
	background-color: #ffffff;
	display: flex;
	align-items: center;
	padding: 0 30rpx;
	position: relative;
}

.back-btn {
	position: absolute;
	left: 30rpx;
}

.title {
	flex: 1;
	text-align: center;
	font-size: 32rpx;
	font-weight: bold;
	color: #333;
}

.settings-list {
	padding: 20rpx;
}

.settings-group {
	background-color: #ffffff;
	border-radius: 12rpx;
	margin-bottom: 20rpx;
	overflow: hidden;
}

.group-title {
	font-size: 28rpx;
	color: #999;
	padding: 20rpx 30rpx;
}

.menu-item {
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding: 30rpx;
	border-bottom: 1rpx solid #f0f0f0;
	
	&:last-child {
		border-bottom: none;
	}
}

.menu-item-left {
	display: flex;
	align-items: center;
	
	.iconfont {
		font-size: 40rpx;
		margin-right: 20rpx;
		color: #666;
	}
	
	text {
		font-size: 28rpx;
		color: #333;
	}
}

.menu-item-right {
	display: flex;
	align-items: center;
	
	.value {
		font-size: 28rpx;
		color: #999;
		margin-right: 10rpx;
	}
	
	.iconfont {
		font-size: 32rpx;
		color: #999;
	}
}

.logout-btn {
	margin: 40rpx 20rpx;
	height: 88rpx;
	line-height: 88rpx;
	text-align: center;
	background-color: #ffffff;
	border-radius: 12rpx;
	color: #FF3B30;
	font-size: 32rpx;
}

/* 遮罩层 */
.mask {
	position: fixed;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	background-color: rgba(0, 0, 0, 0.4);
	z-index: 999;
}

/* 弹窗 */
.popup {
	position: fixed;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%);
	z-index: 1000;
	width: 600rpx;
}

.popup-content {
	background-color: #ffffff;
	border-radius: 12rpx;
	padding: 30rpx;
}

.popup-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 30rpx;
}

.popup-title {
	font-size: 32rpx;
	font-weight: bold;
	color: #333;
}

.close-btn {
	font-size: 40rpx;
	color: #999;
	padding: 10rpx;
	line-height: 1;
}

.input-group {
	margin-bottom: 30rpx;
	
	input {
		height: 88rpx;
		background-color: #f5f5f5;
		border-radius: 8rpx;
		padding: 0 20rpx;
		margin-bottom: 20rpx;
		font-size: 28rpx;
	}
}

.verify-code {
	display: flex;
	gap: 20rpx;
	
	input {
		flex: 1;
	}
	
	.code-btn {
		width: 200rpx;
		height: 88rpx;
		line-height: 88rpx;
		background-color: #1B6EF3;
		color: #ffffff;
		font-size: 26rpx;
		border-radius: 8rpx;
		margin: 0;
		
		&[disabled] {
			background-color: #cccccc;
		}
	}
}

.confirm-btn {
	width: 100%;
	height: 88rpx;
	line-height: 88rpx;
	background-color: #1B6EF3;
	color: #ffffff;
	font-size: 32rpx;
	border-radius: 8rpx;
	margin: 0;
}
</style>
